package com.fau.wmbj.server.controller;

import com.fau.wmbj.server.flowable.service.ActProcessDefinitionService;
import com.fau.wmbj.server.idempotent.annotation.RepeatSubmit;
import com.fau.wmbj.server.utils.Message;
import com.fau.wmbj.server.utils.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 流程定义管理 控制层
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/process-definition")
public class ActProcessDefinitionController {

    private final ActProcessDefinitionService actProcessDefinitionService;


    /**
     * 分页查询
     */
    @PostMapping("/list")
    @ResponseBody
    public Message page(@RequestBody Pageable pageable) {
        return Message.success(actProcessDefinitionService.page(pageable));
    }

    /**
     * 查询历史流程定义列表
     *
     * @param key 流程定义key
     */
    @GetMapping("/getListByKey/{key}")
    public Message getListByKey(@PathVariable String key) {
        return Message.success("操作成功", actProcessDefinitionService.getListByKey(key));
    }

    /**
     * 查看流程定义图片
     *
     * @param processDefinitionId 流程定义id
     */
    @GetMapping("/definition-image/{processDefinitionId}")
    public Message definitionImage(@PathVariable String processDefinitionId) {
        return Message.success("操作成功", actProcessDefinitionService.definitionImage(processDefinitionId));
    }

    /**
     * 查看流程定义xml文件
     *
     * @param processDefinitionId 流程定义id
     */
    @GetMapping("/definitionXml/{processDefinitionId}")
    public Message definitionXml(@PathVariable String processDefinitionId) {
        Map<String, Object> map = new HashMap<>();
        String xmlStr = actProcessDefinitionService.definitionXml(processDefinitionId);
        map.put("xml", Arrays.asList(xmlStr.split("\n")));
        map.put("xmlStr", xmlStr);
        return Message.success(map);
    }

    /**
     * 删除流程定义
     *
     * @param deploymentIds        部署id
     * @param processDefinitionIds 流程定义id
     */
    @DeleteMapping("/delete/{deploymentIds}/{processDefinitionIds}")
    public Message deleteDeployment(@PathVariable List<String> deploymentIds, @PathVariable List<String> processDefinitionIds) {
        return Message.success(actProcessDefinitionService.deleteDeployment(deploymentIds, processDefinitionIds));
    }

    /**
     * 激活或者挂起流程定义
     *
     * @param processDefinitionId 流程定义id
     */
    @RepeatSubmit()
    @PostMapping("/update-state/{processDefinitionId}")
    public Message updateDefinitionState(@PathVariable String processDefinitionId) {
        return Message.success(actProcessDefinitionService.updateDefinitionState(processDefinitionId));
    }

    /**
     * 迁移流程定义
     *
     * @param currentProcessDefinitionId 当前流程定义id
     * @param fromProcessDefinitionId    需要迁移到的流程定义id
     */
    @RepeatSubmit()
    @PostMapping("/migration/{currentProcessDefinitionId}/{fromProcessDefinitionId}")
    public Message migrationDefinition(@PathVariable String currentProcessDefinitionId, @PathVariable String fromProcessDefinitionId) {
        return Message.success(actProcessDefinitionService.migrationDefinition(currentProcessDefinitionId, fromProcessDefinitionId));
    }

    /**
     * 流程定义转换为模型
     *
     * @param processDefinitionId 流程定义id
     */
    @RepeatSubmit()
    @PutMapping("/convertToModel/{processDefinitionId}")
    public Message convertToModel(@PathVariable String processDefinitionId) {
        return Message.success(actProcessDefinitionService.convertToModel(processDefinitionId));
    }

    /**
     * 通过zip或xml部署流程定义
     *
     * @param file         文件
     * @param categoryCode 分类
     */
    @PostMapping("/deployByFile")
    public void deployByFile(@RequestParam("file") MultipartFile file, @RequestParam("categoryCode") String categoryCode) {
        actProcessDefinitionService.deployByFile(file, categoryCode);
    }

}
